package com.example.arithmeticleetcode.leetcode.jan;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-15 09:18
 **/
public class Demo12 {




    public int removeStones2(int[][] stones) {
        if (stones == null || stones.length < 2) return 0;
        Set<Integer> indexSet = new HashSet<>();
        Set<Integer> set = new HashSet<>();
        int count = 0;

        while (indexSet.size() < stones.length) {
            set.clear();
            boolean flag = true;
            for (int i = 0; i < stones.length; i++) {
                if (indexSet.contains(i)) continue;
                if (flag) {
                    count++;
                    set.add(stones[i][0]);
                    set.add(stones[i][1]);
                    indexSet.add(i);
                    flag = false;
                }
                if ((set.contains(stones[i][0]) || set.contains(stones[i][1]))) {
                    set.add(stones[i][0]);
                    set.add(stones[i][1]);
                    indexSet.add(i);
                }
            }
        }
        return stones.length - count;
    }


    public int removeStones(int[][] stones) {
        if (stones == null || stones.length < 2) return 0;
        Set<Integer> indexSet = new HashSet<>();
        Set<Integer> set = new HashSet<>();
        int count = 0;

        while (indexSet.size() < stones.length) {
            set.clear();
            boolean flag = true;
            for (int i = 0; i < stones.length; i++) {
                if (flag) {
                    count++;
                    set.add(stones[i][0]);
                    set.add(stones[i][1]);
                    indexSet.add(i);
                    flag = false;
                }
                if (indexSet.contains(i)) continue;
                if ((set.contains(stones[i][0]) || set.contains(stones[i][1]))) {
                    set.add(stones[i][0]);
                    set.add(stones[i][1]);
                    indexSet.add(i);
                }
            }
        }
        return count;
    }


    /**
     * 1598. 文件夹操作日志搜集器
     *
     * @param logs
     * @return
     */
    public int minOperations(String[] logs) {
        int count = 0;
        for (String log : logs) {
            if ("../".equals(log)) continue;
            if ("./".equals(log)) {
                if (count > 0) {
                    count--;
                }
            } else {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
//        int[][] stones = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}};
//        new Demo12().removeStones(stones);

        int[] customers = {8, 3};
        int[] customers2 = {1, 1, 0, 0, 1, 0, 0, 0, 0};
        minOperationsMaxProfit(customers, 5, 6);
        minOperationsMaxProfit(customers2, 3, 1);
    }


    /**
     * 1599. 经营摩天轮的最大利润
     *
     * @param customers
     * @param boardingCost
     * @param runningCost
     * @return
     */
    public static int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        //统计每次之后的剩余人数
        int remainSum = 0;
        //轮转次数
        int runCount = 0;
        //最佳轮转次数
        int bestRun = 0;
        //最大花费
        int maxCost = 0;
        //最大盈利
        int bestCost = maxCost;
        for (int i = 0; i < customers.length; i++) {
            int tempPeople = 0;
            if (customers[i] < 5) {
                if (customers[i] == 4) {
                    tempPeople = customers[i];
                } else {
                    tempPeople = customers[i];
                    if (remainSum > 0) {
                        int needPeople = 4 - customers[i];
                        if (remainSum > needPeople) {
                            remainSum -= needPeople;
                            tempPeople = 4;
                        } else {
                            tempPeople = customers[i] + remainSum;
                        }
                    }
                }
            } else {
                tempPeople = 4;
                remainSum += (customers[i] - 4);
            }
            runCount++;
            maxCost += (tempPeople * boardingCost - runningCost);
            if (bestCost < maxCost) {
                bestCost = maxCost;
                bestRun = runCount;
            }
        }
        while (remainSum > 0) {
            if (remainSum - 4 <= 0) {
                runCount++;
                maxCost += (remainSum * boardingCost - runningCost);
                if (bestCost < maxCost) {
                    bestCost = maxCost;
                    bestRun = runCount;
                }
                break;
            }
            runCount++;
            maxCost += (4 * boardingCost - runningCost);
            if (bestCost < maxCost) {
                bestCost = maxCost;
                bestRun = runCount;
            }
            remainSum -= 4;
        }
        return bestCost > 0 ? bestRun : -1;

    }
}
